//
//  WCStatusService.m
//  WeboCon
//
//  Created by black sun on 12-3-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WCStatusService.h"

@implementation WCStatusService
-(void)timelineStatusLaterThan:(NSNumber *)statusId{
    NSString *url = [NSString stringWithFormat:@"%@statuses/home_timeline.json",Sina_Base_URL_V2];
    if (statusId) {
        url = [url appendQueryParameter:[NSDictionary dictionaryWithObjectsAndKeys:DefaultSizePerPageString,@"count",[statusId stringValue],@"since_id",nil]];
    }
    ASIHTTPRequest *request = [WCApiRequest getRequestWithApiURL:url needOAuth:YES];
    request.userInfo = [NSDictionary dictionaryWithObject:TimelineStatusLater forKey:kRequestTypeKey];
    request.delegate = self;
    [workingRequest addObject:request];
    [request startSynchronous];
    [NSString stringWithFormat:@"%d",50];
}

-(void)timelineStatusOlderThan:(NSNumber *)statusId{
    NSString *url = [NSString stringWithFormat:@"%@statuses/home_timeline.json",Sina_Base_URL_V2];
    if (statusId) {
        url = [url appendQueryParameter:[NSDictionary dictionaryWithObjectsAndKeys:DefaultSizePerPageString,@"count",[statusId stringValue],@"max_id",nil]];
    }
    ASIHTTPRequest *request = [WCApiRequest getRequestWithApiURL:url needOAuth:YES];
    request.userInfo = [NSDictionary dictionaryWithObject:TimelineStatusOlder forKey:kRequestTypeKey];
    request.delegate = self;
    [workingRequest addObject:request];
    [request startSynchronous];
}

-(void)metionStatus{
    NSString *url = [NSString stringWithFormat:@"%@statuses/mentions.json",Sina_Base_URL_V2];
    ASIHTTPRequest *request = [WCApiRequest getRequestWithApiURL:url needOAuth:YES];
    request.userInfo = [NSDictionary dictionaryWithObject:MetionStatus forKey:kRequestTypeKey];
    request.delegate = self;
    [workingRequest addObject:request];
    [request startSynchronous];
}





-(void)requestSuccess:(ASIHTTPRequest *)request withJSON:(NSDictionary *)json{
    NSString *action = [request.userInfo objectForKey:kRequestTypeKey];
    if ([action isEqualToString:TimelineStatusLater] || [action isEqualToString:TimelineStatusOlder]) {
        NSArray *statusArray = [json objectForKey:@"statuses"];
        NSMutableArray *statuses = [NSMutableArray arrayWithCapacity:statusArray.count];
        for (NSDictionary *dict in statusArray) {
            Status *status = [[Status alloc] init];
            [WCModelHelper populateValues:status fromDictionary:dict];
            NSDictionary *userDict = [dict objectForKey:@"user"];
            NSNumber *uid = [userDict objectForKey:@"id"];
            User *user = [User entityWithId:uid]?:[User entityFromCoreDataContext];
            status.user = user;
            [statuses addObject:status];
        }
        [self.delegate requestDidFinishedWithResult:statuses andUserInfo:request.userInfo];
    }
}

@end
